﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net.Http;

namespace ArgusPCLib.Bird
{
	public enum ServerResponses
	{
		SuccessResponse,
		InvalidServerAdress,
		InvalidResponseStatusCode,
		FailureResponse
	}

	public static class MailServer
	{
		//public static Uri Adress = new Uri("https://example.com/Bird.php");

		public static async Task<ServerResponses> Send(Uri serverAdress, string subject, string messageTxt, string messageHtml, byte[] attachment, string attachmentName)
		{
			if (serverAdress == null)
				return ServerResponses.InvalidServerAdress;

			using (HttpClient httpClient = new HttpClient())
			{
				MultipartFormDataContent content = new MultipartFormDataContent();
				if (!string.IsNullOrEmpty(subject))
					content.Add(new StringContent(subject), POST.Subject);
				if (!string.IsNullOrEmpty(messageTxt))
					content.Add(new StringContent(messageTxt), POST.MessageTxt);
				if (!string.IsNullOrEmpty(messageHtml))
					content.Add(new StringContent(messageHtml), POST.MessageHtml);
				if (attachment != null && attachment.Length > 0)
					content.Add(new ByteArrayContent(attachment), POST.Attachment);
				if (string.IsNullOrEmpty(attachmentName))
					attachmentName = "Attachment";
				content.Add(new StringContent(attachmentName), POST.AttachmentName);

				HttpResponseMessage response = await httpClient.PostAsync(serverAdress, content);
				if (!response.IsSuccessStatusCode)
					return ServerResponses.InvalidResponseStatusCode;

				string str = await response.Content.ReadAsStringAsync();

				// This depends on your server's response. This code assumes, that Bird.php is used.
				if (str == "1")
					return ServerResponses.SuccessResponse;
				else
					return ServerResponses.FailureResponse;
			}
		}
	}
}
